<?php


namespace app\blog\controller;


use app\blog\model\GoodsModel;
use app\blog\validate\GoodsValidate;
use cmf\controller\AdminBaseController;
use think\facade\Db;
use think\Request;

class GoodsController extends AdminBaseController
{
    //显示首页
    public function index(){
        //获取查询数据
        $name = input('name');
        $where = [];
        //判断是否有值
        if($name){
            $where[] = ['name','like','%'.$name.'%'];
        }
        //查询排序分页
        $list = GoodsModel::order('id DESC')
            ->where($where)
            ->paginate(10);
        return $this->fetch('index',[
            'list' => $list,
        ]);
    }
    //添加
    public function add(){
        $list = GoodsModel::select();
        return $this->fetch('add',[
            'list' => $list,
        ]);
    }
    public function addPost(){
        $data = input('post.');
        $data['photos_url'] = implode(',', $data['photos_url']);
        $validate = new GoodsValidate();
        $result = $validate->scene('add')->check($data);
        if(true !== $result){
            $this->error($validate->getError());
        }
        //入库
        if(GoodsModel::create($data)){
            $this->success('添加成功','Goods/index');
        }else{
            $this->error('添加失败');
        }
    }
    //修改
    public function edit(){
        $id = input('id');
        $list = GoodsModel::find($id);
        return $this->fetch('edit',[
            'list' => $list,
        ]);
    }
    public function editPost(){
        $data = input('post.');
        //验证器
        $validate = new GoodsValidate();
        $result = $validate->scene('edit')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }
        //查询是否有此记录
        $list = GoodsModel::find($data['id']);
        if(!$list){
            $this->error('未查询到该记录');
        }
        if($list->update($data)){
            $this->success('修改成功','Goods/index');
        }else{
            $this->error('修改失败');
        }
    }
    //删除
    public function delete(){
        $id = input('id/d','');
        $del = GoodsModel::destroy($id);
        if($del){
            $this->success('删除成功','Goods/index');
        }else{
            $this->error('删除失败');
        }
    }
    //显示隐藏内容
    public function isShow(Request $request){
        $id = $request->param('id');
        if(!$id){
            $this->error('未查询到该id');
        }
        $goods = GoodsModel::find($id);
        $source = null;
        if($goods['source'] == 1){
            $source = 0;
        }
        if($goods['source'] == 0){
            $source = 1;
        }
        if(GoodsModel::update(['source'=>$source],['id'=>$id])){
            $this->success('修改成功','Goods/index');
        }else{
            $this->error('修改失败');
        }
    }
    //打开配置商品属性页面
    public function GoodsDetails2(){
        $goodsId = input('goods_id');
        $this->assign('goods_id',$goodsId);
        return $this->fetch('goods_details2',);
    }
    //配置商品显示页面
    public function GoodsDetails(){
        $goodsId = input('goods_id');
        $list = Db::name('goods_details')
            ->where('id',$goodsId)
            ->select();
        return json(['code' => 1, 'msg' => 'ok', 'data' => $list]);
//        halt($list);
        // $this->assign('goods_id',$goodsId);
//        return $this->fetch('goods_details',[
//            'list' => $list
//        ]);
    }
    //配置提交
    public function GoodsDetailsPost(){
        $attrs = input('post.attr');
        $goodsId = input('post.goods_id');
        if(count($attrs)-1 < 1){
            $this->error('请至少配置一个属性！');
        }
        Db::name('goods_details')
            ->where('goods_id',$goodsId)
            ->delete();
        $insert_data = [];
        foreach($attrs as $attr){
            $insert_data[] = [
                'goods_id' => $goodsId,
                'name' => $attr['name']
            ];
        }
        Db::name('goods_details')->insertAll($insert_data);
        return success('保存成功','Goods/index');
    }
    //打开配置商品属性值页面
    public function GoodsDetailsValue(){
        $goodsId = input('goods_id');
        $this->assign('goods_id',$goodsId);
        return $this->fetch('goods_info');
    }
    //配置提交
    public function GoodsDetailsValuePost(){
        $attrs = input('post.attr');
        $goodsId = input('post.goods_id');
        if(count($attrs)-1 < 1){
            $this->error('请至少配置一个属性值！');
        }
        Db::name('goods_spe')
            ->where('goods_id',$goodsId)
            ->delete();
        $insert_data = [];
        foreach($attrs as $attr){
            $insert_data[] = [
                'goods_id' => $goodsId,
                'name' => $attr['name']
            ];
        }
        Db::name('goods_spe')->insertAll($insert_data);
        return success('保存成功','Goods/index');
    }
    //商品规格配置页面显示2
    public function GoodsInfo(){
        $goodsId = input('goods_id');
        $goods = GoodsModel::field('id,goods_list')->find($goodsId);

        $goodsAttr = json_decode($goods['goods_list'],true);
        return json(['code'=>1,'msg'=>'ok','data'=>$goodsAttr]);
//        halt($goodsAttr);
//        foreach ($goodsAttr as $key => $val){
//            $goodsAttr[$key]['attr_value'] = explode(',',$val['attr_value']);
//        }
//        $goods['goods_list'] = $goodsAttr;
//        $this->assign('goods_id',$goodsId);
//        return $this->fetch('goods_info',[
//            'goodsList' => $goods
//        ]);
    }
    //商品规格配置提交
    public function GoodsInfoPost(){
        $attrs = input('post.attr');
        $goodsId = input('post.goods_id');
        if(count($attrs) < 1) {
            $this->error('请至少配置一个属性值');
        }
//        GoodsModel::where('id',$goodsId)->find();
//        $insert_data = [];
//        foreach($attrs as $attr){
//            $insert_data[] = [
//                'goods_list' => json_encode($attr,JSON_UNESCAPED_UNICODE)
//            ];
//        }
        Db::name('goods')->where('id', $goodsId)->update(['goods_list' => json_encode($attrs,JSON_UNESCAPED_UNICODE)]);
        return success('保存成功','Goods/index');

    }
}